# Tool/tool.py（在你原有代码基础上添加）
import requests


class ApiRequestTool:
    def __init__(self, base_url):
        self.base_url = base_url
        self.session = requests.Session()

    #     self.headers = {  # 原有 headers 不变
    #         "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/90.0.4430.93",
    #         "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"
    #     }
    #     self.token = None  # 新增：存储 Token
    #
    # # 新增：登录并提取 Token 的方法
    # def login_and_get_token(self, login_path, login_data):
    #     full_url = self.base_url + login_path
    #     response = self.session.post(full_url, data=login_data, headers=self.headers)
    #     resp_json = response.json()
    #
    #     # 从登录响应中提取 Token 并添加到请求头
    #     if resp_json.get("status") == 1 and "token" in resp_json.get("data", {}):
    #         self.token = resp_json["data"]["token"]
    #         self.headers["Authorization"] = f"Bearer {self.token}"  # 关键：携带 Token
    #         return True
    #     return False



    # 原有 get/post 方法不变（会自动携带含 Token 的 headers）
    def get(self, path, params=None, head=None):
        full_url = self.base_url + path
        response = self.session.get(full_url, params=params, headers=head)
        return response.status_code, response.text,response.json()

    def post(self, path, data=None , head=None):
        full_url = self.base_url + path
        response = self.session.post(full_url, data=data, headers=head)
        return response.status_code, response.text,response.json()

    def put(self, path, data=None,head=None):
        full_url = self.base_url + path
        response = self.session.put(url=full_url, data=data)
        return response.status_code, response.text

    def delete(self, path, params=None,head=None):
        full_url = self.base_url + path
        response = self.session.delete(url=full_url, params=params)
        return response.status_code, response.text